home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / pthd-0.000 / pthd-0 / pthd-0.7 / src_condv / condv.c next >
Encoding:
C/C++ Source or Header  |  1995-08-16  |  2.1 KB  |  98 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <pthread.h>
  4. #include "utils.h"
  5.  
  6. #define NUM_THREADS ((int) 1)
  7.  
  8. static void init_locks( pthread_mutex_t *mu, pthread_cond_t *cv );
  9. static void destroy_locks( pthread_mutex_t *mu, pthread_cond_t *cv );
  10. static void th_proc( void );
  11.  
  12. static pthread_mutex_t mu;
  13. static pthread_cond_t cv;
  14. static pthread_t **th;
  15.  
  16. static int threads_remaining;
  17.  
  18. int
  19. main( int argc, char *argv[] )
  20. {
  21.    int i, st, thread_count = NUM_THREADS;
  22.  
  23.    if( argc == 2 )
  24.        thread_count = atoi( argv[1] );
  25.  
  26.    threads_remaining = thread_count;
  27.  
  28.    init_locks( &mu, &cv );
  29.  
  30.    /*
  31.     *  --  Make a bunch of threads, each of which will block at the
  32.     *      condition variable.
  33.     */
  34.    th = (pthread_t **) malloc( thread_count * sizeof( pthread_t * ));
  35.    for(i = 0; i < thread_count; i++ )
  36.    {
  37.        th[i] = malloc( sizeof( pthread_t ));
  38.        st = pthread_create( th[i], NULL, (thread_proc_t) th_proc, NULL );
  39.        CHECK(st, "pthread_create()");
  40.    }
  41.  
  42.    st = pthread_cond_broadcast( &cv );
  43.    CHECK(st, "pthread_cond_signal()");
  44.  
  45.    destroy_locks( &mu, &cv );
  46.    for(i = 0; i < thread_count; i++ )
  47.        free( th[i] );
  48.  
  49.    free( th );
  50.    printf(">> Threads remaining %d\n", threads_remaining );
  51.    return( EXIT_SUCCESS );
  52. }
  53.  
  54. static void 
  55. th_proc( void )
  56. {
  57.    int st;
  58.  
  59.    st = pthread_mutex_lock( &mu );
  60.    CHECK(st, "pthread_mutex_lock()");
  61.  
  62.    st = pthread_cond_wait( &cv, &mu );
  63.    CHECK(st, "pthread_cond_wait()");
  64.  
  65.    threads_remaining -= 1;
  66.  
  67.    st = pthread_mutex_unlock( &mu );
  68.    CHECK(st, "pthread_mutex_unlock()");
  69.  
  70.    pthread_exit( THREAD_SUCCESS );
  71. }
  72.  
  73. static void
  74. init_locks( pthread_mutex_t *mu, pthread_cond_t *cv )
  75. {
  76.    int st;
  77.  
  78.    st = pthread_cond_init( cv, NULL );
  79.    CHECK( st, "pthread_cond_init()");
  80.  
  81.    st = pthread_mutex_init( mu, NULL );
  82.    CHECK( st, "pthread_mutex_init()");
  83. }
  84.  
  85. static void
  86. destroy_locks( pthread_mutex_t *mu, pthread_cond_t *cv )
  87. {
  88.    int st;
  89.  
  90.    st = pthread_cond_destroy( cv );
  91.    CHECK( st, "pthread_cond_destroy()");
  92.  
  93.    st = pthread_mutex_destroy( mu );
  94.    CHECK( st, "pthread_mutex_destroy()");
  95.  
  96.    pthread_exit( THREAD_SUCCESS );
  97. }
  98.